package com.zczc.service.impl;

import com.zczc.dao.api.MessageUserDao;
import com.zczc.entity.MessageUser;
import com.zczc.service.MessageUserService;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 16:53 2018-12-12
 * @Modify by:
 */
@Service
public class MessageUserServiceImpl implements MessageUserService {

  @Autowired
  private MessageUserDao messageUserDao;

  @Override
  public Boolean addMessageUser(List<String> userIds, List<String> messageIds) throws Exception {
    if(messageIds!=null&& messageIds.size()>0 && userIds!=null && userIds.size()>0){
      Integer count=0;
      for (String messageId : messageIds) {
        for (String userId : userIds) {
          MessageUser messageUser=new MessageUser();
          messageUser.setId(UUID.randomUUID().toString());
          messageUser.setMessageId(messageId);
          messageUser.setUserId(userId);
          messageUser.setDeleted(false);
         boolean flag= messageUserDao.addMessageUser(messageUser)>0;
         //如果消息添加成功，就向队列中添加一条消息id
         count=flag?++count:count;
        }
      }
      if (count==userIds.size()*messageIds.size()){
        return true;
      }else {
        throw new Exception("总共推送了"+userIds.size()*messageIds.size()+"条记录，成功了"+count+"条记录");
      }

    }
    return false;
  }

  @Override
  public Boolean deleteMessageByMessageIdLogic(List<String> messageIds) {
    if(messageIds!=null && messageIds.size()>0){
      return messageUserDao.deleteMessageByMessageIdLogic(messageIds)>0;

    }
    return false;
  }
}
